package com.apex.sdk.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.databinding.DataBindingUtil;

import com.apex.bluetooth.callback.GeneralCallback;
import com.apex.bluetooth.callback.SleepBloodMonitorCallback;
import com.apex.bluetooth.core.EABleManager;
import com.apex.bluetooth.enumeration.EABleConnectState;
import com.apex.bluetooth.enumeration.QueryWatchInfoType;
import com.apex.bluetooth.model.EABleSleepBloodSwitch;
import com.apex.sdk.R;
import com.apex.sdk.databinding.ActivitySleepBloodBinding;
import com.apex.sdk.dialog.SleepBloodMonitorReportTimeDialog;
import com.apex.sdk.dialog.SwitchDialog;
import com.apex.sdk.dialog.WaitingDialog;


public class SleepBloodMonitorActivity extends AppCompatActivity {
    private WaitingDialog waitingDialog;
    private int intervalTime;
    private int sw;
    SwitchDialog switchDialog;
    SleepBloodMonitorReportTimeDialog sleepBloodMonitorReportTimeDialog;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                if (sw == 1) {
                    activitySleepBloodBinding.restScreen.setText(getString(R.string.switch_state_on));
                } else {
                    activitySleepBloodBinding.restScreen.setText(getString(R.string.switch_state_close));
                }
                activitySleepBloodBinding.intervalTime.setText(intervalTime + getString(R.string.minute));
            } else if (msg.what == 0x41) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(SleepBloodMonitorActivity.this, getString(R.string.failed_to_get_data), Toast.LENGTH_SHORT).show();
            } else if (msg.what == 0x42) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
            } else if (msg.what == 0x43) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(SleepBloodMonitorActivity.this, getString(R.string.add_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };
    ActivitySleepBloodBinding activitySleepBloodBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activitySleepBloodBinding = DataBindingUtil.setContentView(this, R.layout.activity_sleep_blood);
        activitySleepBloodBinding.setLifecycleOwner(this);
        setContentView(activitySleepBloodBinding.getRoot());
        activitySleepBloodBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activitySleepBloodBinding.tool);
        getSupportActionBar().setTitle(null);
        activitySleepBloodBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        EABleConnectState state = EABleManager.getInstance().getDeviceConnectState();
        if (state == EABleConnectState.STATE_CONNECTED) {
            if (waitingDialog == null) {
                waitingDialog = new WaitingDialog(SleepBloodMonitorActivity.this);
            }
            waitingDialog.show();
            EABleManager.getInstance().queryWatchInfo(QueryWatchInfoType.sleep_blood_monitor, new SleepBloodMonitorCallback() {
                @Override
                public void sleepBloodMonitor(EABleSleepBloodSwitch eaBleSleepBloodSwitch) {
                    if (mHandler != null) {
                        Message message = mHandler.obtainMessage();
                        message.what = 0x40;
                        message.arg1 = eaBleSleepBloodSwitch.getSw();
                        message.arg2 = eaBleSleepBloodSwitch.getInterval();
                        sw = eaBleSleepBloodSwitch.getSw();
                        intervalTime = eaBleSleepBloodSwitch.getInterval();
                        mHandler.sendMessage(message);
                    }
                }

                @Override
                public void mutualFail(int errorCode) {
                    if (mHandler != null) {
                        mHandler.sendEmptyMessage(0x41);
                    }
                }
            });
        }
        activitySleepBloodBinding.restScreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (switchDialog == null) {
                    switchDialog = new SwitchDialog(SleepBloodMonitorActivity.this);
                    switchDialog.setSelectListener(new SwitchDialog.SelectListener() {
                        @Override
                        public void selectData(String sex) {
                            activitySleepBloodBinding.restScreen.setText(sex);
                            if (sex.equalsIgnoreCase(getString(R.string.switch_state_on))) {
                                sw = 1;
                            } else {
                                sw = 0;
                            }

                        }
                    });
                }
                if (!switchDialog.isShowing()) {
                    switchDialog.show();
                }

            }
        });
        activitySleepBloodBinding.intervalTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sleepBloodMonitorReportTimeDialog == null) {
                    sleepBloodMonitorReportTimeDialog = new SleepBloodMonitorReportTimeDialog(SleepBloodMonitorActivity.this);
                    sleepBloodMonitorReportTimeDialog.setSelectListener(new SleepBloodMonitorReportTimeDialog.SelectListener() {
                        @Override
                        public void selectData(int sex) {
                            activitySleepBloodBinding.intervalTime.setText(sex + getString(R.string.minute));
                            intervalTime = sex;
                        }
                    });
                }
                if (!sleepBloodMonitorReportTimeDialog.isShowing()) {
                    sleepBloodMonitorReportTimeDialog.show();
                }
            }
        });
        activitySleepBloodBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
                    EABleSleepBloodSwitch eaBleSleepBloodSwitch = new EABleSleepBloodSwitch();
                    eaBleSleepBloodSwitch.setSw(sw);
                    eaBleSleepBloodSwitch.setInterval(intervalTime);
                    EABleManager.getInstance().startSleepBloodMonitor(eaBleSleepBloodSwitch, new GeneralCallback() {
                        @Override
                        public void result(boolean success, int reason) {
                            if (mHandler != null) {
                                mHandler.sendEmptyMessage(0x42);
                            }
                        }

                        @Override
                        public void mutualFail(int errorCode) {
                            if (mHandler != null) {
                                mHandler.sendEmptyMessage(0x43);
                            }
                        }
                    });
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }
        if (sleepBloodMonitorReportTimeDialog != null) {
            sleepBloodMonitorReportTimeDialog.destroyDialog();
            sleepBloodMonitorReportTimeDialog = null;
        }
        if (switchDialog != null) {
            switchDialog.destroyDialog();
            switchDialog = null;
        }
        super.onDestroy();
    }
}
